﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.MapInfo.Model;
using System.IO;

public partial class MapInfo_DepartmentCarNew : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            Bind(0);
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (Request["DepartmentID"] == null)
            return;

        string DepartmentID = Request["DepartmentID"];

       
        string CarName = this.txtCarName.Text;
        int CarCount = int.Parse(this.txtCarCount.Text);
        string CarID = this.txtCarID.Text;
        string Remark = this.txtRemark.Text;

       // string PicUrl = this.txtPicUrl.Text;

        if (CheckPic().Trim() != "")
        {
            DepartmentCarInfo model = new DepartmentCarInfo();
            model.CarName = CarName;
            model.CarCount = CarCount;
            model.CarID = CarID;
            model.Remark = Remark;
            model.PicUrl = CheckPic();
            model.DepartmentID = DepartmentID;
            if (Yipusoft.MapInfo.BLL.DepartmentCar.Add(model) > 0)
                lblPromt.Text = "添加成功";

            Bind(0);
        }
        else
        {
            lblPromt.Text = "请选择车辆图片!";
            return ; 
        }
    }
    public void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        Bind(PageIndex);
    }

    private void Bind( int PageIndex)
    {
        if (Request["DepartmentID"] == null)
            return;
        string DepartmentID = Request["DepartmentID"];
        List<Yipusoft.MapInfo.Model.DepartmentCarInfo> list = Yipusoft.MapInfo.BLL.DepartmentCar.GetList("DepartmentID='" + DepartmentID + "'");
        List<Yipusoft.MapInfo.Model.DepartmentCarInfo> list2 = Yipusoft.MapInfo.BLL.DepartmentCar.GetPhotoByDepartmentID("DepartmentID='" + DepartmentID + "'", "ID", "ASC", 6, PageIndex);
        this.DltList.DataSource = list2;
        this.ListPageContral1.ItemCount = list.Count;
        this.DltList.DataBind();
    }

    public string CheckPic()
    {
        int _limitwidth = 800;
        int _limitheight = 600;

        if (this.FilePic.FileName != "" && FilePic.PostedFile.ContentLength > 0)
        {

            // if not already present, create a directory named /Uploads/<CurrentUserName>
            string filename = FilePic.FileName.ToLower();
            int i = filename.LastIndexOf(".");
            filename = filename.Substring(i, filename.Length - i);

            if (!(filename == ".jpg" || filename == ".jpeg" || filename == ".gif" || filename == ".png" || filename == ".bmp"))
            {

                lblPromt.Text = "不受支持的类型,请重新选择!";
                return "";

            }//检查上传文件的格式是否有效

            Stream oStream = FilePic.PostedFile.InputStream;
            System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);

            int owidth = oImage.Width; //原图宽度
            int oheight = oImage.Height; //原图高度

            if (owidth > _limitwidth || oheight > _limitheight)
            {

                lblPromt.Text = "超过允许的图片尺寸(" + _limitwidth.ToString() + "×" + _limitheight.ToString() + ")范围!";
                return "";
            }//检查是否超出规定尺寸

            string dirUrl = this.Request.ApplicationPath +
                          "Uploads/Car/" + Request["departmentID"];
            string dirPath = Server.MapPath(dirUrl);
            if (!Directory.Exists(dirPath))
                Directory.CreateDirectory(dirPath);
            // save the file under the user's personal folder
            string fileUrl = dirUrl + "/" + DateTime.Now.ToString("msyyyyMMddhhssmmms")+filename;
            FilePic.PostedFile.SaveAs(Server.MapPath(fileUrl));

            return fileUrl;

        }
        else
        {
            lblPromt.Text = "请选择车辆图片!";
            return "";

        }
       
    
    }
    protected void BtnDelete_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName == null)
            return;
        try
        {
            Yipusoft.MapInfo.BLL.DepartmentCar.Delete(int.Parse(e.CommandName.ToString()));
            Bind(0);
        }
        catch {  };
    }
}
